﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Common;

namespace CSC440TeamOne.Admin.Views
{
    public partial class ToleranceConfigDetails : Microsoft.Practices.CompositeWeb.Web.UI.Page, IToleranceConfigDetailsView
    {
        private ToleranceConfigDetailsPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                var tempInt = 0;

                if (int.TryParse(Request.Params["ID"], out tempInt))
                {
                    HealthParameterID = tempInt;
                }

                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            _presenter.OnSave();
        }

        [CreateNew]
        public ToleranceConfigDetailsPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	


        public int HealthParameterID
        {
            get
            {
                var value = ViewState["HealthParameterID"];

                var result = int.MinValue;

                if (value != null && int.TryParse(value.ToString(), out result))
                {
                    result = Convert.ToInt32(value);
                }
                return result;
            }
            set
            {
                ViewState["HealthParameterID"] = value;
            }
        }

        public void Show(Common.ToleranceConfigDetailsViewModel model)
        {
            HealthParameter healthParameter = model.HealthParameter;

            if (healthParameter != null)
            {
                mvState.SetActiveView(vwRead);

                Name = healthParameter.Name;

                Description = healthParameter.Description;

                Formula = healthParameter.Formula;
            }
        }


        public void Update(Common.ToleranceConfigDetailsViewModel model)
        {
            HealthParameter healthParameter = model.HealthParameter;

            if (healthParameter != null)
            {
                healthParameter.Name = Name;

                healthParameter.Description = Description;

                healthParameter.Formula = Formula;
            }
        }

        public void OnSaveCompleted(Common.ToleranceConfigDetailsViewModel model)
        {
            mvState.SetActiveView(vwSaveCompleted);
        }

        public string Name
        {
            get
            {
                return txtName.Text;
            }
            set
            {
                txtName.Text = value;

                litName.Text = value;
            }
        }

        public string Description
        {
            get
            {
                return txtDescription.Text;
            }
            set
            {
                txtDescription.Text = value;

                litDescription.Text = value;
            }
        }

        public string Formula
        {
            get
            {
                return txtFormula.Text;
            }
            set
            {
                txtFormula.Text = value;

                litFormula.Text = value;
            }
        }


        public void ShowEditView()
        {
            mvState.SetActiveView(vwEdit);
        }
    }
}

